﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Web.Configuration;
using System.Configuration;
using System.Web;

using Ionic.Zip;

namespace Core.Utils
{
    public class Files
    {
        public static byte[] FileToArray(string sFilePath)
        {
            if (File.Exists(sFilePath))
            {
                using (FileStream fs = new FileStream(sFilePath, FileMode.Open, FileAccess.Read))
                {
                    System.IO.BinaryReader br = new System.IO.BinaryReader(fs);
                    
                        return br.ReadBytes((Int32)fs.Length);
                    
                }
            }
            return null;
        }

        public static byte[] GetZipFile(Dictionary<string, string> imagesToZip)
        {
            return GetZipFile(imagesToZip);
        }

        public static byte[] GetZipFile(Dictionary<string,string> imagesToZip, string appPath)
        {
            using (ZipFile zip = new ZipFile())
            {
                int i = 0;
                foreach (KeyValuePair<string, string> imageToZip in imagesToZip)
                {
                    i++;
                    string filePath = (!string.IsNullOrEmpty(appPath)) ? appPath + imageToZip.Key : HttpContext.Current.Server.MapPath(imageToZip.Key);
                    FileInfo fi = new FileInfo(filePath);

                    zip.AddEntry(imageToZip.Value.Replace('/','_') + " - " + i.ToString("D3") + ".jpg", fi.OpenRead());
                }

                byte[] outputData = null;

                using (var memoryStream = new MemoryStream())
                {
                    zip.Save(memoryStream);
                    outputData = memoryStream.ToArray();
                }
                return outputData;
            }
        }

        public static byte[] GetZipFile(string folderToZip, string folderName)
        {
            DirectoryInfo di = new DirectoryInfo(HttpContext.Current.Server.MapPath(folderToZip));
            if (di.Exists)
            {
                FileInfo[] fis = di.GetFiles("*.jpg", SearchOption.AllDirectories);

                using (ZipFile zip = new ZipFile())
                {
                    for (int i = 0; i <= fis.Count() - 1; i++)
                    {
                        FileInfo fi = fis[i];
                        zip.AddEntry(folderName.Replace('/', '_') + " - " + (i + 1).ToString("D3") + ".jpg", fi.OpenRead());
                    }

                    byte[] outputData = null;

                    using (var memoryStream = new MemoryStream())
                    {
                        zip.Save(memoryStream);
                        outputData = memoryStream.ToArray();
                    }
                    return outputData;
                }
            }
            return null;
        }

        public static void GenerateZipFile(string folderToZip, string folderToSave, string fileName)
        {
            DirectoryInfo di = new DirectoryInfo(folderToZip);
            if (di.Exists)
            {
                FileInfo[] fis = di.GetFiles("*.jpg", SearchOption.AllDirectories);

                using (ZipFile zip = new ZipFile())
                {
                    for (int i = 0; i <= fis.Count() - 1; i++)
                    {
                        FileInfo fi = fis[i];
                        zip.AddEntry(fileName.Replace('/', '_') + " - " + (i + 1).ToString("D3") + ".jpg", fi.OpenRead());
                    }

                    zip.Save(folderToSave);
                }
            }
        }

        public static void ByteArrayToFile(string filePath, byte[] data)
        {
            using (FileStream fileStream = new FileStream(filePath, FileMode.Create, FileAccess.Write))
            {
                fileStream.Write(data, 0, data.Length);
            }
        }

        public static int GetMaxRequestLength()
        {
            // presume that the section is not defined in the web.config
            int result = 4096;

            HttpRuntimeSection section = ConfigurationManager.GetSection("system.web/httpRuntime") as HttpRuntimeSection;
            if (section != null) result = section.MaxRequestLength;

            return result * 1024;
        }
    }
}
